namespace Filters.Demo.FilterForms
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using AForge.Imaging.Filters;
    using Filters.Demo.Core.FilterConfiguration;

    /// <summary>
    /// Summary description for ContrastForm.
    /// </summary>
    public class ContrastForm : System.Windows.Forms.Form
    {
        private ContrastCorrection filter = new ContrastCorrection();

        private FilterElement filterData = new FilterElement { Name = "Contrast Correction", Type = typeof(ContrastCorrection).AssemblyQualifiedName };
        private FilterParameterElement filterParameter = new FilterParameterElement { Name = "Factor", Type = "double" };

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox contrastBox;
        private System.Windows.Forms.TrackBar contrastTrackBar;
        private System.Windows.Forms.GroupBox groupBox1;
        private Filters.Demo.FilterControls.FilterPreview filterPreview;
        private System.Windows.Forms.Button cancelButton;
        private System.Windows.Forms.Button okButton;
        private System.ComponentModel.Container components = null;

        // Constructor
        public ContrastForm()
        {
            // Required for Windows Form Designer support
            this.InitializeComponent();

            this.contrastBox.Text = this.filter.Factor.ToString();
            this.contrastTrackBar.Value = (int)(this.filter.Factor * 1000);

            this.filterPreview.Filter = this.filter;

            this.filterData.FilterParameters.FilterParameters.Add(this.filterParameter);
            this.filterParameter.Value = this.filter.Factor.ToString();
        }

        // Image property
        public Bitmap Image
        {
            set { this.filterPreview.Image = value; }
        }

        // Filter property
        public IFilter Filter
        {
            get { return this.filter; }
        }

        // FilterData property
        public FilterElement FilterData
        {
            get { return this.filterData; }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.components != null)
                {
                    this.components.Dispose();
                }
            }

            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.contrastBox = new System.Windows.Forms.TextBox();
            this.contrastTrackBar = new System.Windows.Forms.TrackBar();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.filterPreview = new Filters.Demo.FilterControls.FilterPreview();
            this.cancelButton = new System.Windows.Forms.Button();
            this.okButton = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.contrastTrackBar)).BeginInit();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(10, 13);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(100, 14);
            this.label1.TabIndex = 0;
            this.label1.Text = "Contrast factor:";
            // 
            // contrastBox
            // 
            this.contrastBox.Location = new System.Drawing.Point(110, 10);
            this.contrastBox.Name = "contrastBox";
            this.contrastBox.Size = new System.Drawing.Size(50, 20);
            this.contrastBox.TabIndex = 1;
            this.contrastBox.Text = string.Empty;
            this.contrastBox.TextChanged += new System.EventHandler(this.ContrastBoxTextChanged);
            // 
            // contrastTrackBar
            // 
            this.contrastTrackBar.Location = new System.Drawing.Point(10, 40);
            this.contrastTrackBar.Maximum = 5000;
            this.contrastTrackBar.Minimum = 1;
            this.contrastTrackBar.Name = "contrastTrackBar";
            this.contrastTrackBar.Size = new System.Drawing.Size(250, 42);
            this.contrastTrackBar.TabIndex = 17;
            this.contrastTrackBar.TickFrequency = 200;
            this.contrastTrackBar.Value = 1;
            this.contrastTrackBar.ValueChanged += new System.EventHandler(this.ContrastTrackBarValueChanged);
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.filterPreview});
            this.groupBox1.Location = new System.Drawing.Point(270, 5);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(160, 165);
            this.groupBox1.TabIndex = 18;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Preview";
            // 
            // filterPreview
            // 
            this.filterPreview.Image = null;
            this.filterPreview.Location = new System.Drawing.Point(10, 15);
            this.filterPreview.Name = "filterPreview";
            this.filterPreview.Size = new System.Drawing.Size(140, 140);
            this.filterPreview.TabIndex = 12;
            // 
            // cancelButton
            // 
            this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.cancelButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.cancelButton.Location = new System.Drawing.Point(150, 143);
            this.cancelButton.Name = "cancelButton";
            this.cancelButton.TabIndex = 20;
            this.cancelButton.Text = "&Cancel";
            // 
            // okButton
            // 
            this.okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.okButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.okButton.Location = new System.Drawing.Point(60, 143);
            this.okButton.Name = "okButton";
            this.okButton.TabIndex = 19;
            this.okButton.Text = "&Ok";
            // 
            // ContrastForm
            // 
            this.AcceptButton = this.okButton;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.cancelButton;
            this.ClientSize = new System.Drawing.Size(439, 178);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.cancelButton,
																		  this.okButton,
																		  this.groupBox1,
																		  this.contrastTrackBar,
																		  this.contrastBox,
																		  this.label1});
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "ContrastForm";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Contrast";
            ((System.ComponentModel.ISupportInitialize)(this.contrastTrackBar)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);
        }

        #endregion

        // value of contrast track bar changed
        private void ContrastTrackBarValueChanged(object sender, System.EventArgs e)
        {
            this.contrastBox.Text = ((double)this.contrastTrackBar.Value / 1000).ToString();
        }

        // value of contrast text box changed
        private void ContrastBoxTextChanged(object sender, System.EventArgs e)
        {
            try
            {
                this.filter.Factor = double.Parse(this.contrastBox.Text);
                this.filterParameter.Value = this.contrastBox.Text;

                this.filterPreview.RefreshFilter();
            }
            catch (Exception exeption)
            {
                MessageBox.Show(exeption.Message);
            }
        }
    }
}
